package com.tarena.lbs.coupon.dao.repository.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.coupon.dao.mapper.UserCouponsMapper;
import com.tarena.lbs.coupon.dao.repository.UserCouponsRepository;
import com.tarena.lbs.coupon.pojo.po.UserCouponsPO;
import com.tarena.lbs.coupon.pojo.query.UserCouponCodeQuery;
import com.tarena.lbs.coupon.pojo.query.UserCouponQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserCouponsRepositoryImpl implements UserCouponsRepository {
    @Autowired
    private UserCouponsMapper userCouponsMapper;


    @Override
    public void Insert(UserCouponsPO userCouponsPO) {
        userCouponsMapper.insert(userCouponsPO);
    }

    @Override
    public PageResult<UserCouponsPO> pageList(UserCouponQuery couponQuery) {
        QueryWrapper<UserCouponsPO> queryWrapper = new QueryWrapper<>();
        if (couponQuery.getUserId() != null){
            queryWrapper.eq("user_id", couponQuery.getUserId());
        }
        if (couponQuery.getStatus() != null){
            queryWrapper.eq("status", couponQuery.getStatus());
        }
        if (StringUtils.isNotBlank(couponQuery.getOrderNo())){
            queryWrapper.like("order_no", couponQuery.getOrderNo());
        }
        if (couponQuery.getReceiveChannel() != null){
            queryWrapper.eq("receive_channel", couponQuery.getReceiveChannel());
        }
        if (couponQuery.getCouponType() != null){
            queryWrapper.eq("coupon_type", couponQuery.getCouponType());
        }
         List<UserCouponsPO> list =userCouponsMapper.selectList(queryWrapper);
        PageResult<UserCouponsPO> pageResult = new PageResult<>();
        pageResult.setTotal(userCouponsMapper.selectCount(queryWrapper));
        pageResult.setObjects(list);
        return pageResult;
    }

    @Override
    public UserCouponsPO selectUserCoupon(UserCouponCodeQuery query) {
        QueryWrapper<UserCouponsPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", query.getUserId());
        return userCouponsMapper.selectOne(queryWrapper);

    }
}

